﻿(function ($) {

    $.fn.metro_grid = function (options) {

        var defaults = {
            initCell: function (cell, row, colum) { }
        };
        // Extend our default options with those provided.    
        var opts = $.extend(defaults, options);
        // Our plugin implementation code goes here.   

        return this.each(function () {
            var $this = $(this);

            var column_total_width = 0;
            $('.metro_grid_column', $this).each(function (column_index) {
                var $column = $(this);

                if ($column.hasClass('hide')) {
                    return;
                }

                var column_width = $(this).attr('data-width');

                if (isNaN(column_width)) {
                    column_width = $column.outerWidth();
                    $('.metro_grid_row', $this).each(function () {
                        var $row = $(this);
                        //auto_column_width = $('.metro_grid_cell:eq(' + column_index + ')', $row).outerWidth();
                        var cell_width = $('.metro_grid_cell:eq(' + column_index + ')', $row).outerWidth();
                        if (cell_width > column_width)
                            column_width = cell_width;
                    });
                    $column.width(column_width);
                }

                $column.width(column_width);

                column_total_width += $column.outerWidth();

                $('.metro_grid_row', $this).each(function (row_index) {

                    var $row = $(this);

                    var $cell = $($('.metro_grid_cell', $row)[column_index]);
                    $cell.width(column_width);
                    $('input', $cell).width(column_width);
                    $('select', $cell).width(column_width);

                    opts.initCell($cell, $row, $column);
                });
            });
            $('.metro_grid_contain', $this).width(column_total_width);

            $('.metro_grid_row:odd', $this).each(function () {
                if (!$(this).hasClass('odd')) {
                    $(this).addClass('odd');
                }
            });
            $('.metro_grid_row:even', $this).each(function () {
                if (!$(this).hasClass('even')) {
                    $(this).addClass('even');
                }
            });

            $('.metro_grid_row', $this).unbind('mouseenter').bind('mouseenter', function () {
                $(this).addClass('hover');
            });
            $('.metro_grid_row', $this).unbind('mouseleave').bind('mouseleave', function () {
                $(this).removeClass('hover');
            });

            $('.metro_grid_row', $this).each(function () {
                var $first_cell = $('.metro_grid_cell:first', $(this));
                if (!$first_cell.hasClass('first')) {
                    $first_cell.addClass('first');
                }
                var $last_cell = $('.metro_grid_cell:last', $(this));
                if (!$last_cell.hasClass('last')) {
                    $last_cell.addClass('last');
                }
            });

            $('.metro_grid_row', $this).unbind('click').bind('click', function () {
                $('.metro_grid_row:not(.hover)', $this).removeClass('selected');
                $(this, $this).addClass('selected');
            });
            if ($this.attr('initialized') == undefined) {
                $('.metro_grid_column:not(:last)', $this).prepend('<span class="metro_grid_column_separator"></span>');
                $('.metro_grid_column:first', $this).addClass('first');
                $('.metro_grid_column:last', $this).addClass('last');

                $this.attr('initialized', 'true');
            }
        });
    };
})(jQuery);